package org.subatu.orm.service.base;


import org.subatu.orm.query.base.CondEnum;
import org.subatu.orm.query.function.FunctionToQueryColumnUtil;
import org.subatu.orm.query.function.SFunction;
import org.subatu.orm.query.table.CondColumn;
import org.subatu.orm.query.table.QueryColumn;

import java.util.List;

/**
 * 比较条件
 */
public interface Condition extends BaseQuery {

    // ********* 以下是构造 关系条件 *********
    default ConditionAndOrder and() {
        return makeCondition(CondEnum.and);
    }

    default ConditionAndOrder or() {
        return makeCondition(CondEnum.or);
    }

    default ConditionAndOrder boxL() {
        return makeCondition(CondEnum.boxL);
    }

    default ConditionAndOrder boxR() {
        return makeCondition(CondEnum.boxR);
    }

    // ********* 以下是构造 对比 条件 *********

    default <E> ConditionAndOrder eq(SFunction<E> column, Object value) {
        return eq(true, column, value);
    }

    default <E> ConditionAndOrder eq(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.eq, value);
    }

    default <E> ConditionAndOrder gt(SFunction<E> column, Object value) {
        return gt(true, column, value);
    }

    default <E> ConditionAndOrder gt(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.gt, value);
    }

    default <E> ConditionAndOrder ge(SFunction<E> column, Object value) {
        return ge(true, column, value);
    }

    default <E> ConditionAndOrder ge(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.ge, value);
    }

    default <E> ConditionAndOrder lt(SFunction<E> column, Object value) {
        return lt(true, column, value);
    }

    default <E> ConditionAndOrder lt(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.lt, value);
    }

    default <E> ConditionAndOrder le(SFunction<E> column, Object value) {
        return le(true, column, value);
    }

    default <E> ConditionAndOrder le(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.le, value);
    }

    default <E> ConditionAndOrder in(SFunction<E> column, Object value) {
        return in(true, column, value);
    }

    default <E> ConditionAndOrder in(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.in, value);
    }

    default <E> ConditionAndOrder notIn(SFunction<E> column, Object value) {
        return notIn(true, column, value);
    }

    default <E> ConditionAndOrder notIn(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.notIn, value);
    }

    default <E> ConditionAndOrder isNull(SFunction<E> column) {
        return makeCondition(true, column, CondEnum.isNull, null);
    }

    default <E> ConditionAndOrder notNull(SFunction<E> column) {
        return makeCondition(true, column, CondEnum.notNull, null);
    }

    default <E> ConditionAndOrder likeL(SFunction<E> column, Object value) {
        return likeL(true, column, value);
    }

    default <E> ConditionAndOrder likeL(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.likeL, value);
    }

    default <E> ConditionAndOrder likeR(SFunction<E> column, Object value) {
        return likeR(true, column, value);
    }

    default <E> ConditionAndOrder likeR(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.likeR, value);

    }

    default <E> ConditionAndOrder like(SFunction<E> column, Object value) {
        return like(true, column, value);
    }

    default <E> ConditionAndOrder like(boolean isCheck, SFunction<E> column, Object value) {
        return makeCondition(isCheck, column, CondEnum.like, value);

    }

    // ********* 以下是实际执行 *********

    // 创建 关系队列
    default ConditionAndOrder makeCondition(CondEnum relationEnum) {
        // 构造 关系
        CondColumn condColumn = new CondColumn(relationEnum);
        // 加入到条件队列
        this.getCondList().add(condColumn);
        return (ConditionAndOrder) this;
    }

    // 创建 查询队列
    default <E> ConditionAndOrder makeCondition(boolean isCheck, SFunction<E> function, CondEnum condEnum, Object value) {
        QueryColumn queryColumn = FunctionToQueryColumnUtil.getQueryColumn(function);
        // 构造 查询条件
        CondColumn condColumn = new CondColumn(isCheck, queryColumn, condEnum, value);
        // 加入到条件队列
        this.getCondList().add(condColumn);
        return (ConditionAndOrder) this;
    }

    // 获取 查询条件
    default List<CondColumn> getCondList() {
        return sqlData().whereList;
    }

}
